import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DeleteDemo {
    public static void main(String[] args) throws SQLException {
        //1.创建好数据源
        DataSource dataSource = new MysqlDataSource();
        //设置数据库所在地址
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf-8&useSSL=false");
        //设置登录数据库的用户名
        ((MysqlDataSource) dataSource).setUser("root");
        //设置登录数据库的密码
        ((MysqlDataSource) dataSource).setPassword("989811");

        //2.让代码和数据库服务器建立连接
        Connection connection = dataSource.getConnection();

        //3.操作数据库
        String sql = "delete from student where id = 9";
        PreparedStatement statement = connection.prepareStatement(sql);

        //4.执行SQL
        /**
         * insert, delete 用executeUpdate()
         * select 用executeQuery()
         */
        statement.executeUpdate();

        //5.释放资源
        statement.close();//先
        connection.close();//后
    }
}
